package cn.itcast.bos.web.action.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.bos.web.action.common.BaseAction;
import cn.itcast.crm.domain.Customer;

@SuppressWarnings("serial")
@Controller
@Scope("prototype")
@Namespace("/")
@ParentPackage("json-default")
public class FixedAreaAction extends BaseAction<FixedArea> {

	@Autowired
	private FixedAreaService fixedAreaService;

	private Integer page;
	private Integer rows;

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	@Action(value = "fixedArea_add", results = { @Result(name = "success", type = "redirect", location = "./pages/base/fixed_area.html") })
	public String addFixedArea() {

		fixedAreaService.addFixedArea(getModel());
		return SUCCESS;
	}

	@Action(value = "fixedArea_findPageData", results = { @Result(name = "success", type = "json") })
	public String getAllFixedArea() {

		PageRequest pageRequest = new PageRequest(page - 1, rows);
		Specification<FixedArea> specification = new Specification<FixedArea>() {

			@Override
			public Predicate toPredicate(Root<FixedArea> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {

				List<Predicate> predicates = new ArrayList<>();
				if (StringUtils.isNotBlank(model.getId())) {

					Predicate predicate1 = cb.equal(
							root.get("id").as(String.class), model.getId());
					predicates.add(predicate1);
				}

				if (StringUtils.isNotBlank(model.getCompany())) {

					Predicate predicate2 = cb.like(
							root.get("company").as(String.class),
							"%" + model.getCompany() + "%");
					predicates.add(predicate2);
				}

				return cb.and(predicates.toArray(new Predicate[0]));
			}
		};

		Page<FixedArea> pageData = fixedAreaService.findPageData(specification,
				pageRequest);
		pushPageDataToValueStack(pageData);

		return SUCCESS;
	}
	
	
	
	@Action(value = "findNoAssociationCustomers", results = {@Result(name = "success", type = "json")})
	public String findNoAssociationCustomers() {

		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9002/crm_management/services/customerService/noassociationcustomers")
				.accept(MediaType.APPLICATION_JSON)
				.getCollection(Customer.class);
		
		ServletActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
	
	
	@Action(value = "findAssociationCustomers", results = {@Result(name = "success", type = "json")})
	public String findAssociationCustomers() {

		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9002/crm_management/services/customerService/associationfixedareacustomers/"+model.getId())
				.accept(MediaType.APPLICATION_JSON)
				.type(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		
		ServletActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
	
	private String[] customerIds;
	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}

	@Action(value = "decidedzone_assigncustomerstodecidedzone", results = { @Result(name = "success", type = "redirect", location = "./pages/base/fixed_area.html")})
	public String assignCustomersToDecidedzone() {

		String fixedAreaId = model.getId();
		String customerIdStr = StringUtils.join(customerIds, ",");
		WebClient
		.create("http://localhost:9002/crm_management/services/customerService/associationcustomerstofixedarea?customerIdStr="+customerIdStr+"&fixedAreaId="+fixedAreaId)
				.put(null);
		
		System.out.println("assignCustomersToDecidedzone----fixedAreaId----->"+fixedAreaId);
		System.out.println("assignCustomersToDecidedzone----customerIds----->"+customerIdStr);
		
		return SUCCESS;
	}
	
	private Integer courierId;
	private Integer takeTimeId;
	
	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}

	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

	/*
	 * 关联快递员到定区 courierId,takeTimeId
	 */
	@Action(value="fixedArea_associationCourierToFixedArea", results={@Result(name="success", type="redirect", location = "./pages/base/fixed_area.html")})
	public String associationCourierToFixedArea() {
		
		fixedAreaService.associationCourierToFixedArea(courierId, takeTimeId, model);
		return SUCCESS;
	}
	
	
	
	
	
	
	
	
	
	
	

}
